import datetime

from deps.voidsdk.django.modelautocode import models
from deps.voidsdk.pytoolset.datetime import now
from deps.voidsdk.pytoolset.enum import Enum, Sex


EducationLevel = Enum((
    (0, "UNKNOWN", "未知"),
    (1, "TO_HIGH_SCHOOL", "高中及以下"),
    (2, "SPECIAL_SCHOOL", "中专"),
    (3, "JUNIOR_COLLEGE", "大专"),
    (4, "UNDER_GRADUATE", "本科"),
    (5, "MASTER", "硕士"),
    (6, "DOCTOR", "博士"),
))

IncomeLevel = Enum((
    (0, "UNKNOWN", "未知"),
    (1, "LT_3000", "3000以下"),
    (2, "BE_3001_AND_5000", "3001-5000"),
    (3, "BE_5001_AND_8000", "5001-8000"),
    (4, "BE_8001_AND_12000", "8001-12000"),
    (5, "BE_12001_AND_20000", "12001-20000"),
    (6, "BE_20001_AND_50000", "20001-50000"),
    (7, "GT_50001", "50001+"),
))

MaritalStatus = Enum((
    (1, "UNMARRIED", "未婚"),
    (2, "DISSOCIATION", "离异"),
    (3, "WID", "丧偶"),
))

MemberLevel = Enum((
    (0, "NONE", "非会员"),
    (1, "BASIC", "基础会员")
))

ImageOrVideo = Enum((
    (0, "IMAGE", "照片"),
    (1, "VIDEO", "视频")
))


class User(models.Model):
    sex = models.IntegerField("性别", choices=Sex.choice_tuples())
    is_supporter = models.BooleanField("是否亲友团")

    favorite_dish = models.CharField("最喜欢的菜", max_length=200, blank=True, default="")
    favorite_famous = models.CharField("最喜欢的名人", max_length=200, blank=True, default="")
    favorite_song = models.CharField("最喜欢的一首歌", max_length=200, blank=True, default="")
    favorite_book = models.CharField("最喜欢的一本书", max_length=200, blank=True, default="")
    special_habbit = models.CharField("最与众不同的爱好", max_length=200, blank=True, default="")

    province = models.CharField("省", max_length=100, blank=True, default="")
    city = models.CharField("市", max_length=100, blank=True, default="")
    area = models.CharField("区", max_length=100, blank=True, default="")
    area_id = models.IntegerField("行政区划ID", blank=True, default=1)
    birth_day = models.DateField("生日", blank=True, null=True)
    height = models.IntegerField("身高", blank=True, default=0)
    weight = models.IntegerField("体重", blank=True, default=0)
    education = models.IntegerField("学历", choices=EducationLevel.choice_tuples(),
            blank=True, default=EducationLevel.UNKNOWN)
    income = models.IntegerField("收入水平", choices=IncomeLevel.choice_tuples(),
            blank=True, default=IncomeLevel.UNKNOWN)

    phone = models.CharField("手机号", max_length=100, blank=True, default="")
    password = models.CharField("密码", max_length=200, blank=True, default="")
    nickname = models.CharField("昵称", max_length=200, blank=True, default="")
    avatar = models.CharField("头像", max_length=200, blank=True, default="")
    marital_status = models.IntegerField("婚姻状况", choices=MaritalStatus.choice_tuples(),
            blank=True, default=MaritalStatus.UNMARRIED)
    have_child = models.BooleanField("是否有小孩", blank=True, default=False)
    is_want_child = models.BooleanField("是否想要小孩", blank=True, default=False)
    career = models.CharField("职业", max_length=200, blank=True, default="")
    house_status = models.CharField("买房情况", max_length=200, blank=True, default="")
    car_status = models.CharField("买车情况", max_length=200, blank=True, default="")
    native_place = models.CharField("籍贯", max_length=200, blank=True, default="")
    is_smork = models.BooleanField("是否吸烟", blank=True, default=False)
    is_drink = models.BooleanField("是否喝酒", blank=True, default=False)
    nation = models.CharField("民族", max_length=100, blank=True, default="")
    marry_plan = models.CharField("何时结婚", max_length=100, blank=True, default="")

    client_type = models.CharField("客户端类型", max_length=200, blank=True, default="")
    client_version = models.CharField("客户端版本", max_length=200, blank=True, default="")

    cover = models.CharField("个人封面", max_length=200, blank=True, default="")
    statement = models.CharField("内心独白", max_length=500, blank=True, default="")
    member_level = models.IntegerField("会员等级", choices=MemberLevel.choice_tuples(),
            blank=True, default=MemberLevel.NONE)
    is_realname = models.BooleanField("是否实名认证", blank=True, default=False)

    create_timestamp = models.DateTimeField("创建时间", auto_now_add=True)
    last_update_timestamp = models.DateTimeField("最后修改时间", auto_now=True)

    def __str__(self):
        return self.nickname

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = "用户"


class ApiAccessToken(models.Model):
    user = models.ForeignKey(User, verbose_name="用户")
    access_token = models.CharField(u"access_token", max_length=300)
    expire_timestamp = models.DateTimeField("过期时间")

    create_timestamp = models.DateTimeField("创建时间", auto_now_add=True)
    last_update_timestamp = models.DateTimeField("最后修改时间", auto_now=True)

    @property
    def expired(self):
        # 提前10分钟过期
        return now() + datetime.timedelta(seconds=600) >= self.expire_timestamp

    class Meta:
        verbose_name = "api_access_token"
        verbose_name_plural = "api_access_token"


VerifyCodeType = Enum((
    (1, "MANUAL_VERIFY", "人工验证"),
    (2, "PHONE_VERIFY", "手机验证")
))


class VerifyCode(models.Model):
    phone_id = models.CharField("手机标识", max_length=200)
    code = models.CharField("验证码", max_length=30, default="", blank=True)
    t = models.IntegerField("验证码类型", choices=VerifyCodeType.choice_tuples())
    note = models.CharField("备注", max_length=30, default="", blank=True)

    sms_send_timestamp = models.DateTimeField("短信发送时间", blank=True, null=True)

    create_timestamp = models.DateTimeField(auto_now_add=True)
    last_update_timestamp = models.DateTimeField(auto_now=True)

    @property
    def expired(self):
        # 10分钟过期
        if self.sms_send_timestamp:
            return now() - datetime.timedelta(seconds=300) >= self.sms_send_timestamp
        else:
            return False

    def __str__(self):
        return self.phone_id + ":" + self.code

    class Meta:
        verbose_name = u"验证码"


class Photos(models.Model):
    user = models.ForeignKey(User, verbose_name="用户")
    url = models.CharField("链接", max_length=300, blank=True, default="")
    type = models.IntegerField("照片/视频", choices=ImageOrVideo.choice_tuples(), blank=True, default=0,
        help_text="0-照片, 1-视频")
    create_timestamp = models.DateTimeField(auto_now_add=True)
    last_update_timestamp = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user.nickname

    class Meta:
        verbose_name = "相册"
        verbose_name_plural = verbose_name


class HopeSpouse(models.Model):
    user = models.ForeignKey(User, verbose_name="用户")
    age_min = models.IntegerField("最小年龄", blank=True, default=0)
    age_max = models.IntegerField("最大年龄", blank=True, default=0)
    province = models.CharField("省", max_length=100, blank=True, default="")
    city = models.CharField("市", max_length=100, blank=True, default="")
    area = models.CharField("区", max_length=100, blank=True, default="")
    area_id = models.IntegerField("行政区划ID", blank=True, default=1)
    height = models.IntegerField("身高", blank=True, default=0)
    weight = models.IntegerField("体重", blank=True, default=0)
    education = models.IntegerField("学历", choices=EducationLevel.choice_tuples(),
            blank=True, default=EducationLevel.UNKNOWN)
    income = models.IntegerField("月收入", choices=IncomeLevel.choice_tuples(),
            blank=True, default=IncomeLevel.UNKNOWN)
    marital_status = models.IntegerField("婚姻状况", choices=MaritalStatus.choice_tuples(),
            blank=True, default=MaritalStatus.UNMARRIED)
    have_child = models.BooleanField("是否有小孩", blank=True, default=False)
    is_want_child = models.BooleanField("是否想要小孩", blank=True, default=False)
    is_smork = models.BooleanField("是否吸烟", blank=True, default=False)
    have_photo = models.BooleanField("有无照片", blank=True, default=False)
    create_timestamp = models.DateTimeField(auto_now_add=True)
    last_update_timestamp = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user.nickname

    class Meta:
        verbose_name = "择偶条件"
        verbose_name_plural = verbose_name
